package com.simon.bigdata.mapreduce.wordcount;


import com.simon.bigdata.mapreduce.wordcount.combiner.WordCountCombiner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * Created by changf on 2017/7/27.
 */
public class TestWordCount {


    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration=new Configuration();
       /* configuration.set("fs.defaultFs","hdfs://hdp02:9000");
        configuration.set("mapreduce.framework.name","yarn");
        configuration.set("yarn.resource.hostname","mini1");*/
        //-DHADOOP_USER_NAME=ROOT

        Job job = Job.getInstance(configuration);

//        System.setProperty("hadoop.home.dir", "D:\\hadoops");
        //System.setProperty("HADOOP_USER_NAME", "root");


        //linux
        //job.setJar("/root/wordcount.jar");
        //windows 本地运行
        job.setJarByClass(TestWordCount.class);
        job.setMapperClass(WordCounterMapper.class);
        job.setReducerClass(WordCounterReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);


        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);


        //新增combiner 每个mapTask都会调用自己的组件 这里可以进行combiner组件的设置
        job.setCombinerClass(WordCountCombiner.class);
        //设置读入位置
       /* FileInputFormat.setInputPaths(job,"/wordcount/input");
        FileOutputFormat.setOutputPath(job,new Path("/wordcount/output"));
*/
        FileInputFormat.setInputPaths(job,"D:/wordcount/input");
        FileOutputFormat.setOutputPath(job,new Path("D:/wordcount/output"));


        //向yarn集群提交这个job
        boolean res = job.waitForCompletion(true);
        System.exit(res?0:1);

    }


}
